# 1424. 对角线遍历 II

const { array } = require("yargs");

var findDiagonalOrder = function(nums) {
  let arrays = [],
    result = [];

  for (let i = 0; i < nums.length; i++) {
    // for (let j = nums[i].length - 1; j >= 0; j--) {
    //   if (!arrays[i + j]) arrays[i + j] = [];
    //   arrays[i + j].push(nums[i][j]);
    // }
    for (let j = 0; j < nums[i].length; j++) {
      if (!arrays[i + j]) arrays[i + j] = [];
      arrays[i + j].push(nums[i][j]);
    }
  }

  console.log(arrays);

  arrays.forEach((array) => (result = result.concat(array)));

  return result;
};

const nums = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

console.log(findDiagonalOrder(nums));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Last Updated: 6/27/2023, 7:40:45 PM